package com.propertypoint.presentation.actions;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.propertypoint.presentation.model.Advertiesment;
import com.propertypoint.presentation.model.Agent;
import com.propertypoint.service.business.AdvertiesmentServiceImpl;
import com.propertypoint.service.general.AdvertiesmentService;
import com.propertypoint.util.PropertyPointConstants;

/**
 * Servlet implementation class AdvertiesmentServlet
 */
@WebServlet("/AdvertiesmentServlet")
public class AdvertiesmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private AdvertiesmentService addService=new AdvertiesmentServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdvertiesmentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String expiryDate=request.getParameter("expiryDate");
		Agent agent=(Agent)request.getSession().getAttribute("user");
		DateFormat sourceFormat = new SimpleDateFormat("dd/MM/yyyy");
		 
		try {
			Date date = sourceFormat.parse(expiryDate);
			Advertiesment add=new Advertiesment();
			add.setEndDate(date);
			add.setAgentId(agent.getId());			 
			addService.inserAdd(add);
			request.setAttribute("post_add_status", "true");
			request.setAttribute("post_add_message", "Your Advertisement Successfully Added!");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		HttpSession session = request.getSession();
		session.setAttribute("tab_status", "advertisement");
		
		RequestDispatcher rd = getServletContext().getRequestDispatcher(
				PropertyPointConstants.HOME_PAGE);
		rd.forward(request, response);
	}

}
